
<template>
  <div>
    <div style="margin-bottom: 10px;">
      <el-input v-model="cardId" placeholder="请输入会员卡ID..."  suffix-icon="el-icon-search" style="width: 200px"></el-input>
      <el-button style="margin-left: 5px;" @click="loadPostByCardId">查询</el-button>
      <el-button @click="resetParam">重置</el-button>
      <el-button style="margin-left: 5px;" @click="add">新增</el-button>
    </div>

    <el-table :data="tableData"
              :header-cell-style="{background:'#eaecf3',color:'rgb(106,117,119)'}" border
    >
      <el-table-column prop="consumptionId" label="消费ID" width="80">
      </el-table-column>
      <el-table-column prop="cardId" label="会员卡ID" width="80">
      </el-table-column>
      <el-table-column prop="serviceId" label="服务ID" width="80">
      </el-table-column>
      <el-table-column prop="therapistId" label="技师ID" width="80">
      </el-table-column>
      <el-table-column prop="originalAmount" label="原价金额" width="80">
      </el-table-column>
      <el-table-column prop="actualAmount" label="实际金额" width="80">
      </el-table-column>
      <el-table-column prop="startTime" label="开始时间" width="180">
      </el-table-column>
      <el-table-column prop="endTime" label="结束时间" width="180">
      </el-table-column>
      <el-table-column prop="status" label="状态" width="110">
      </el-table-column>
      <el-table-column prop="evaluation" label="评价" width="180">
      </el-table-column>
      <el-table-column prop="evaluationScore" label="评分" width="60">
      </el-table-column>
      <el-table-column prop="notes" label="备注" width="180">
      </el-table-column>
      <el-table-column prop="operate" label="操作" width="260" >
        <template slot-scope="scope">
        <el-button @click="mod(scope.row)">编辑</el-button>
        <el-button @click="del(scope.row.consumptionId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
    <el-dialog title="提示" :visible.sync="centerDialogVisible" width="30%" center>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="会员卡ID" prop="cardId">
          <el-col :span="20">
            <el-input v-model="form.cardId"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="服务ID" prop="serviceId">
          <el-col :span="20">
          <el-input v-model="form.serviceId"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="技师ID" prop="therapistId">
          <el-col :span="20">
            <el-input v-model="form.therapistId"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="原价金额" prop="originalAmount">
          <el-col :span="20">
            <el-input v-model="form.originalAmount"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="实际金额" prop="actualAmount">
          <el-col :span="20">
            <el-input v-model="form.actualAmount"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="COMPLETED"></el-radio>
            <el-radio label="CANCELLED"></el-radio>
            <el-radio label="RESERVED"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-col :span="11">
            <el-date-picker type="date" placeholder="选择日期" v-model="form.startTime" style="width: 100%;"></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-col :span="11">
            <el-date-picker type="date" placeholder="选择日期" v-model="form.endTime" style="width: 100%;"></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="评价" prop="evaluation">
          <el-col :span="20">
            <el-input v-model="form.evaluation"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="评分" prop="evaluationScore">
          <el-col :span="20">
            <el-input v-model="form.evaluationScore"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-col :span="20">
            <el-input v-model="form.notes"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
export default {
  name: 'ConsumptionManage',

  data() {
    return {
      tableData: [],
      pageSize:10,
      pageNum:1,
      total:0,
      cardId:'',
      centerDialogVisible: false,
      form: {
        consumptionId:'',
        cardId:'',
        serviceId: '',
        therapistId: '',
        originalAmount: '',
        actualAmount: '',
        startTime:'',
        endTime: '',
        evaluation: '',
        evaluationScore: '',
        notes: '',
        status: ''
      }
    }
  },
  methods:{
    resetForm() {
      this.$refs.form.resetFields();
    },
    mod(row){
      console.log(row)
      this.centerDialogVisible = true
      this.$nextTick(() => {
        this.form=row;
      });
    },
    add() {
      this.centerDialogVisible = true
      this.$nextTick(() => {
        this.resetForm();
      });
    },
    doSave() {
      this.$axios.post(this.$httpUrl + '/consumption/save',this.form).then(res => res.data).then(res => {
        console.log(res)
        if (res.code == 200) {
          this.$message({
            message: '操作成功！',
            type: 'success'
          });
          this.loadPost();
          this.centerDialogVisible = false
        } else {
          this.$message({
            message: '操作失败！',
            type: 'error'
          })
        }
      });
    },
    doMod(){
      this.$axios.post(this.$httpUrl + '/consumption/update',this.form).then(res => res.data).then(res => {
        console.log(res)
        if (res.code == 200) {
          this.$message({
            message: '操作成功！',
            type: 'success'
          });
          this.loadPost();
          this.centerDialogVisible = false
        } else {
          this.$message({
            message: '操作失败！',
            type: 'error'
          })
        }
      });
    },
    save(){
      this.$refs.form.validate((valid)=>{
        if(valid){
          if(this.form.consumptionId){
            this.doMod();
          }else {
            this.doSave();
          }
        }else {
          console.log('error submit');
          return false;
        }
      });
    },
    del(consumptionId) {
      console.log(consumptionId);

      this.$axios.get(this.$httpUrl + '/consumption/delete?consumptionId=' + consumptionId)
          .then(res => res.data)
          .then(res => {
            console.log(res);
            if (res.code == 200) {
              this.$message({
                message: '操作成功！',
                type: 'success'
              });
              this.loadPost();
            } else {
              this.$message({
                message: '操作失败！',
                type: 'error'
              });
            }
          })
          .catch(error => {
            console.error('删除操作出错:', error);
            this.$message({
              message: '操作出错！',
              type: 'error'
            });
          });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum=1
      this.pageSize=val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadPost()
    },
    loadGet(){
      this.$axios.get(this.$httpUrl+'/consummption/list').then(res=>res
          .data).then(res=>{
        console.log(res)
      })
    },
    loadPost(){
      this.$axios.get(this.$httpUrl+'/consumption/listPage',{
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize
        }
      }).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.tableData=res.data
          this.total=res.total
        }else {
          alert('获取数据失败')
        }

      })
    },
    loadPostByCardId(){
      this.$axios.get(this.$httpUrl+'/consumption/listPageByCardId',{
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          cardId:this.cardId
        }
      }).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.tableData=res.data
          this.total=res.total
        }else {
          alert('获取数据失败')
        }

      })
    },
    resetParam() {
      this.cardId = '';
      this.loadPost();
    }
  },
  beforeMount() {
    //this.loadGet();
    this.loadPost();
  }
}
</script>
<style scoped>

</style>